package com.hifar.demo3;

import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Map;

import com.hifar.demo3.model.DmData;
import com.hifar.demo3.model.DmTable;
import com.hifar.demo3.model.ProcedureStep;
import com.hifar.demo3.pojo.TableCell;
import com.hifar.demo3.pojo.TableRow;

public class S1000DIntegrationExample {
    public static void main(String[] args) {
        try {
            // 1. 解析DM文件
            S1000DParser dmParser = new S1000DParser();
            InputStream dmStream = new FileInputStream("DXJG0001A.xml");
            DmData dmData = dmParser.parse(dmStream);
            
            // 2. 解析操作数据文件
            OperationDataParser opParser = new OperationDataParser();
            InputStream opStream = new FileInputStream("DATA-DXJG0001A.xml");
            opParser.parse(opStream);
            
            // 3. 将操作数据解析器设置到DM解析器中
            dmParser.setOperationDataParser(opParser);
            
            // 4. 重新解析DM文件以填充操作数据
            dmStream = new FileInputStream("DXJG0001A.xml");
            dmData = dmParser.parse(dmStream);
            
            // 5. 打印带操作数据的表格
            System.out.println("===== 带操作数据的表格 =====");
            for (DmTable table : dmData.getTables()) {
                System.out.println("\n表格: " + table.getTitle());
                for (TableRow row : table.getRows()) {
                    System.out.print("| ");
                    for (TableCell cell : row.getCells()) {
                        System.out.print(cell.getContent() + " | ");
                    }
                    System.out.println();
                }
            }
            
            // 6. 打印带操作数据的步骤
            System.out.println("\n===== 带操作数据的步骤 =====");
            for (ProcedureStep step : dmData.getMainProcedureSteps()) {
                printStepWithOperationData(step, 0);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void printStepWithOperationData(ProcedureStep step, int indent) {
        StringBuilder indentStr = new StringBuilder();
        for (int i = 0; i < indent; i++) {
            indentStr.append("  ");
        }
        
        System.out.println(indentStr + step.getTitle());
        System.out.println(indentStr + "描述: " + step.getDescription());
        
        // 打印操作数据
        if (!step.getOperationData().isEmpty()) {
            System.out.println(indentStr + "操作数据:");
            for (Map.Entry<String, String> entry : step.getOperationData().entrySet()) {
                System.out.println(indentStr + "  " + entry.getKey() + ": " + entry.getValue());
            }
        }
        
        // 打印子步骤
        if (step.getSubSteps() != null) {
            for (ProcedureStep subStep : step.getSubSteps()) {
                printStepWithOperationData(subStep, indent + 1);
            }
        }
    }
}